
let vec2 = require('gl-matrix').vec2;
let myTools = require('./utils/mytools');

class Vec2{
    constructor(x = 0,y = 0) {
        this.data = vec2.fromValues(x,y);
    }

    get x(){
        return this.data[0];
    }
    get y(){
        return this.data[1];
    }

    set x(x){
        this.data[0] = x;
    }
    set y(y){
        this.data[1] = y;
    }

    set(x,y){
        if(myTools.Types.isObject(x)){
            this.data[0] = x.x;
            this.data[1] = x.y;
        }else{
            this.data[0] = x;
            this.data[1] = y;
        }
    }

    // 顺时针为正
    getPositiveLeft(){
        return new Vec2(-this.y,this.x);
    }

    // 逆时针为负
    getNegtiveLeft(){
        return new Vec2(this.y,-this.x);
    }

    normalize(){
        vec2.normalize(this.data,this.data);
    }

    length(){
        return vec2.len(this.data);
    }

    static dot(v1,v2){
        return vec2.dot(v1.data,v2.data);
    }

    static sub(v1,v2){
        let d = new Vec2();
        vec2.sub(d.data,v1.data,v2.data);
        return d;
    }
}


module.exports = Vec2;